#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/un.h>
#include<string.h>

#define N 64

int main(int argc, const char *argv[])
{
    int sockfd;
    char buf[N];
    struct sockaddr_un serveraddr, clientaddr;

    unlink("socket");

    sockfd = socket(AF_LOCAL, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
        perror("fail to sockfd");
        return -1;
    }

    serveraddr.sun_family = AF_LOCAL;
    strcpy(serveraddr.sun_path, "mysocket");

    clientaddr.sun_family = AF_LOCAL;
    strcpy(clientaddr.sun_path, "socket");

    if(bind(sockfd, (struct sockaddr*)&clientaddr, sizeof(clientaddr)) < 0)
    {
        perror("fail to bind");
        return -1;
    }

    while(1)
    {
        printf("<client>");
        fgets(buf, N, stdin);
        if(sendto(sockfd, buf, N, 0, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0)
        {
            perror("fail to sendto");
            return -1;
        }
        if(strncmp(buf, "quit", 4) == 0)
        {
            break;
        }
        if(recvfrom(sockfd, buf, N, 0, NULL, NULL) < 0)
        {
            perror("fail to recvfrom");
            return -1;
        }
        printf("buf:%s\n", buf);
    }
    close(sockfd);

    return 0;
}